#include "genomicpos.h"
using namespace std;

GenomicPosition::GenomicPosition(std::string chromosome,position_t S,position_t E):
			chromosome(chromosome),
			chromStart(S),
			chromEnd(E)
		{

		}

GenomicPosition::GenomicPosition(const GenomicPosition& cp):
		chromosome(cp.chromosome),
		chromStart(cp.chromStart),
		chromEnd(cp.chromEnd)
	{

	}
GenomicPosition& GenomicPosition::operator=(const GenomicPosition& cp)
	{
	if(this!=&cp)
		{
		chromosome.assign(cp.chromosome);
		chromStart=cp.chromStart;
		chromEnd=cp.chromEnd;
		}
	return *this;
	}

bool GenomicPosition::operator==(const GenomicPosition& cp) const
	{
	return chromosome.compare(cp.chromosome)==0 &&
			chromStart==cp.chromStart &&
			chromEnd==cp.chromEnd;
	}

auto_ptr<GenomicPosition> GenomicPosition::parse(const char* position)
		{
		if(position==NULL) return auto_ptr<GenomicPosition>(NULL);

		string chrom;
	    position_t chromStart;
	    position_t chromEnd;
	    string line(position);
	    string::size_type colon=line.find(':');
	    if(colon==string::npos)
			{
			chrom.assign(line);
			chromStart=0;
			chromEnd=numeric_limits<position_t>::max();
			}
	    else
			{
			chrom.assign(line.substr(0,colon));
			line=line.substr(colon+1);
			string::size_type dash=line.find('-');
			if(dash==string::npos)
				{
				chromStart=atoi(line.c_str());
				chromEnd=chromStart+1;
				}
			else
				{
				chromStart=atoi(line.substr(0,dash).c_str());
				chromEnd=atoi(line.substr(dash+1).c_str());
				}
			}
	    return auto_ptr<GenomicPosition>(new GenomicPosition(chrom,chromStart,chromEnd));
		}
